//
//  HRPlayView.m
//  喜马拉雅FM(高仿品)
//
//  Created by apple-jd33 on 15/11/9.
//  Copyright © 2015年 HansRove. All rights reserved.
//

#import "HRPlayView.h"
#import "ZYPlayerViewController.h"
#import "videoTableViewCell.h"
#import "DetailListModel.h"
@interface HRPlayView ()
// 设置一个私有的定时器
@property (nonatomic,retain) CADisplayLink *link;
@end


//宏定义   角度转弧度
#define angleToRadian(x) (x/180.0*M_PI)
@implementation HRPlayView

-(void)dealloc{
    [_link release];
    [_circleIV release];
    [_playButton release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"playOrStop" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"playndStop" object:nil];
  //  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"playndStop" object:nil];

    [super dealloc];


}

- (instancetype)init {
    if (self = [super init]) {
        // 布局
        
        UIImageView *backgoundIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_np_normal"]];
        [self addSubview:backgoundIV];
        UIImageView *goundIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bofang1"]];
        
        [backgoundIV addSubview:goundIV];
        [backgoundIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        [goundIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        _circleIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fengche"]];
        [backgoundIV addSubview:_circleIV];
        [_circleIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(2);
            make.right.mas_equalTo(-2);
            make.bottom.mas_equalTo(-8);
        }];
        // 作圆背景
        _circleIV.layer.cornerRadius = 30;
        _circleIV.clipsToBounds = YES;
        // 设置circle的用户交互
        backgoundIV.userInteractionEnabled = YES;
        _circleIV.userInteractionEnabled = YES;
        
        [backgoundIV release];
        [_circleIV release];
        //第一次启动小圆钮显示内容
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *str = [defaults objectForKey:@"model"];
       // if ([[defaults objectForKey:@"FirstTime"] isEqualToString:@"YES"]) {
            [_circleIV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"holderr"]];
            [self.playButton setImage:[UIImage imageNamed:@"toolbar_play_n"] forState:UIControlStateNormal];
            self.link.paused = YES;
           
            
            NSLog(@"点击播放");
//            [defaults setObject:@"NO" forKey:@"FirstTime"];
//            [defaults synchronize];
       // }

        
        // 点击前
        [self.playButton setImage:[UIImage imageNamed:@"toolbar_play_n"] forState:UIControlStateNormal];
        //self.isBool = YES;
        //注册个通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playAction:) name:@"playOrStop" object:nil];
        //视频方发送过来的通知
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction:) name:@"playndStop" object:nil];
    }
    return self;
}
//实现通知方法
-(void)playAction:(NSNotification *)noti{
    NSDictionary *dic = [noti object];
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[dic objectForKey:@"picture"] forKey:@"pic"];
    [defaults synchronize];
    if ([[dic objectForKey:@"play"] isEqualToString:@"1"]) {
      //  NSLog(@"========++++++%@",[dic objectForKey:@"picture"]);
        [_circleIV sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"holderr"]];
        [self.playButton setImage:[UIImage imageNamed:@"toolbar_pause_n"] forState:UIControlStateNormal];
        self.link.paused = NO;
    }else{
        [_circleIV sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"holderr"]];
        [self.playButton setImage:[UIImage imageNamed:@"toolbar_play_n"] forState:UIControlStateNormal];
        self.link.paused = YES;

    
    }
    
    NSLog(@"实现通知方法");
}

//视频方发过来的通知实现通知方法
-(void)stopAction:(NSNotification *)noti{
    NSDictionary *dic = [noti object];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[dic objectForKey:@"picture"] forKey:@"pic"];
    [defaults synchronize];
    if ([[dic objectForKey:@"play"] isEqualToString:@"1"]) {
        //  NSLog(@"========++++++%@",[dic objectForKey:@"picture"]);
        //[_circleIV sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"holderr"]];
        [self.playButton setImage:[UIImage imageNamed:@"toolbar_pause_n"] forState:UIControlStateNormal];
        self.link.paused = NO;
    }else{
       // [_circleIV sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"holderr"]];
        [self.playButton setImage:[UIImage imageNamed:@"toolbar_play_n"] forState:UIControlStateNormal];
        self.link.paused = YES;
        
        
    }
    
    NSLog(@"实现通知方法");
}

- (void)rotation {
    self.circleIV.layer.transform = CATransform3DRotate(self.circleIV.layer.transform, angleToRadian(72/60.0), 0, 0, 1);
}

#pragma mark - PlayButton,CADisplayLink定时器懒加载
- (UIButton *)playButton {
    if (!_playButton) {
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
        [self  addSubview:_playButton];
        [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(2);
            make.right.mas_equalTo(-2);
            make.bottom.mas_equalTo(-8);
        }];
        
//        // 按钮点击后做的方法
        [_playButton addTarget:self action:@selector(playButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        
        // 启动定时器
        self.link.paused = NO;
    }
    return _playButton;
}
//播放暂停按钮
-(void)playButtonDidClick{
//    ZYPlayerViewController *zyPlay = [ZYPlayerViewController shareZYPlayerViewController];
    
    
//    //第一次启动小圆钮显示内容
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    NSMutableArray *array = [defaults objectForKey:@"allArray"];
    
    
    if ([[defaults objectForKey:@"FirstPlay"] isEqualToString:@"NO"]) {
        
        //[[NSNotificationCenter defaultCenter] postNotificationName:@"toPlayer" object:nil];
       NSLog(@"firstTime");
      //  [defaults setObject:@"NO" forKey:@"FirstPlay"];
    }else{
        //点圆钮发送通知跳出到播放页面
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ToZYPlayer" object:nil];
    
    }
    
    }

- (CADisplayLink *)link {
    if (!_link) {
        // 创建定时器, 一秒钟调用rotation方法60次
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotation)];
        // 手动将定时器加入到事件循环中
        [_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    }
    return _link;
}



@end
